iT邦幫忙

2024 iThome 鐵人賽

DAY 29
1

上一篇介紹了 dbt Unit Test 概念以及使用的時機,這篇將介紹如何寫 dbt Unit Test,以及如何執行

Unit Testing Model

上一篇有提到如果 model 中有複雜邏輯,可以寫 Unit Test 確保邏輯正確,像是下面的 model 透過正規表達式驗證 Email 是否符合特定格式:

SELECT 
  id,
  name,
  status,
  email,
  IF(REGEXP_CONTAINS(mobile, '^09[0-9]{8}$'), mobile, NULL) AS cellPhone,
  PARSE_DATE('%Y-%m-%d', birthday) AS birthday,
  gender,
  address.detail_address.city AS city,
  address.detail_address.district AS dist,
  address.detail_address.address1 AS address,
  PARSE_DATETIME('%Y-%m-%d %H:%M:%S', created_at) AS created_at,
  PARSE_DATETIME('%Y-%m-%d %H:%M:%S', updated_at) AS updated_at
FROM 
  {{ ref('source.customers') }}

Unit Test 需要定義在 yaml 檔中,需要包含以下項目:

  • 測試名稱
  • 測試的 model 名稱
  • 測試案例:輸入及預期的輸出,可以使用 dictcsvsql 格式寫在 yaml 檔,或是使用 fixture 檔案並放在 tests 資料夾下,詳細可參考 dbt 文件
unit_tests:
  - name: test_is_valid_mobile
    model: datamart.customers
    given:
      - input: ref('source.customers')
        rows:
          - {mobile: 0912345789}
          - {mobile: 912345789}
          - {mobile: (886)912345789}
          - {mobile: 0212345780}

    expect:
      rows:
        - {cellPhone: 0912345789}
        - {cellPhone: null}
        - {cellPhone: null}
        - {cellPhone: null}

需要注意一點,Unit Test 中使用到的上游表,ref('stg_customers')、ref('top_level_email_domains') 對應的表必須存在在資料倉儲中,有兩種做法可以確保資料表存在:

  • dbt empty :執行 dbt Unit Test 前,透過 —empty 建立空的上游表

    dbt run --select "source.customers" --empty
    
    
  • dbt build :執行 dbt build 會依序執行 Unit Test、執行 model(dbt run) 以及對資料做測試(data_test)

如何執行 dbt Unit Test

dbt 有兩種測試:Unit Test 和 Data Test,使用 dbt test 會檢查所有 Test(包含 Unit Test、Data Test)

dbt test --select datamart.customers

如果要只選擇 Unit Test,需要調整 —-select 的條件,指定類別為 Unit 的測試

dbt test --select "datamart.customers,test_type:unit"

或是指定特定的測試名稱

dbt test --select test_is_valid_mobile

如果 Unit Test 案例寫錯或是 SQL 有錯,會呈現以下結果:

image.png

可以看到 Unit Test 的預期結果與預期不符,會顯示錯誤及錯誤的案例

以上就是如何使用 dbt Unit Test 的介紹,希望能幫助大家更了解此功能!

參考


上一篇
dbt 新功能: Unit Test
下一篇
總結及感想
系列文
dbt 修煉之路30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言